package main

import (
	"errors"
	"fmt"
	"gorm.io/gorm"
)

func main5() {

	{
		//[84.769ms] [rows:0] UPDATE `t_student` SET `age`=age+1
		//error WHERE conditions required

		student := Student{} //也是条件
		//student := Student{ID: 5} //也是条件

		tx := DB.Model(&student).UpdateColumn("age", gorm.Expr("age+?", 1))
		if tx.Error != nil {
			if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
				fmt.Println("not found")
			} else {
				fmt.Println("error", tx.Error.Error())
			}
		} else {
			fmt.Println("tx.RowsAffected:", tx.RowsAffected)
			fmt.Println(student)
		}
	}
	fmt.Println("-----------------------")
	{
		student := Student{} //也是条件
		//student := Student{ID: 5} //也是条件

		tx := DB.Model(&student).Update("age", gorm.Expr("age+?", 1))
		if tx.Error != nil {
			if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
				fmt.Println("not found")
			} else {
				fmt.Println("error", tx.Error.Error())
			}
		} else {
			fmt.Println("tx.RowsAffected:", tx.RowsAffected)
			fmt.Println(student)
		}
	}
}
